package com.wgke.bluetooth.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.view.WheelView;
import com.wgke.bluetooth.R;
import com.wgke.bluetooth.utils.CommonUtil;
import com.wgke.bluetooth.utils.ToastUtil;

import java.util.Arrays;
import java.util.stream.Collectors;

public class WheelDialog extends BaseDialog {

    String[] arr;
    String[] names;
    int[] value;

    String title;

    public WheelDialog(@NonNull Context context, String[] arr, String title) {
        super(context);
        this.arr = arr;
        this.title = title;
        this.value = new int[arr.length];
    }

    public WheelDialog(@NonNull Context context, String[] arr, String[] names, String title) {
        super(context);
        this.arr = arr;
        this.title = title;
        this.names = names;
        this.value = new int[arr.length];
    }

    public WheelDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    public int layoutId() {
        return R.layout.dialog_wheelview;
    }

    @Override
    public void initView() {
        findViewById(R.id.tvTitleChoose).setVisibility(TextUtils.isEmpty(title) ? View.GONE : View.VISIBLE);
        ((TextView) findViewById(R.id.tvTitleChoose)).setText("" + title);
        WheelView wv1 = findViewById(R.id.wheelView1);
        WheelView wv2 = findViewById(R.id.wheelView2);
        WheelView wv3 = findViewById(R.id.wheelView3);
        WheelView wv4 = findViewById(R.id.wheelView4);
        wv1.setVisibility(arr.length > 0 ? View.VISIBLE : View.GONE);
        wv2.setVisibility(arr.length > 1 ? View.VISIBLE : View.GONE);
        wv3.setVisibility(arr.length > 2 ? View.VISIBLE : View.GONE);
        wv4.setVisibility(arr.length > 3 ? View.VISIBLE : View.GONE);

        for (int i = 0; i < arr.length; i++) {
            initWheelView(i == 0 ? wv1 : i == 1 ? wv2 : i == 2 ? wv3 : wv4, i);
        }
        findViewById(R.id.tv_confirm).setOnClickListener(v -> {
            if (callbackArr != null) {
                callbackArr.chooseResult(value);
            }
            dismiss();
        });
    }

    public void initWheelView(WheelView wheelView, int index) {
        int[] array = CommonUtil.rangeStringToIntArray(arr[index]);
        if (names != null && index == 0 && array.length == names.length) {
            wheelView.setAdapter(new ArrayWheelAdapter(Arrays.stream(names).collect(Collectors.toList())));
        } else
            wheelView.setAdapter(new ArrayWheelAdapter(Arrays.stream(array).mapToObj(String::valueOf).collect(Collectors.toList())));
        wheelView.setCurrentItem(array.length > 3 ? 2 : 0); // 默认选中第三项
        value[index] = array[array.length > 3 ? 2 : 0];
        wheelView.setOnItemSelectedListener(i -> {
            value[index] = array[i];
        });
        wheelView.setTextSize(20f); // 单位px
        wheelView.setCyclic(true);
        wheelView.setLineSpacingMultiplier(2.5f);

    }
}